package com.mifengs.commonbusiness.base;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengs.commonbusiness.R;
import com.mifengs.commonbusiness.util.ToastUitl;
import com.mifengs.commonbase.base.BasePresenter;
import com.mifengs.commonbase.base.BaseView;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.components.support.RxFragment;

import javax.inject.Inject;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by lihaoqi on 2017/10/30.
 */

public class BaseFragment<T extends BasePresenter> extends RxFragment implements BaseView {


    private Dialog mLoadingDialog;

    @Inject
    protected T mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initInjector();
    }

    public void initInjector() {}


    @Override
    public void showShortToast(int resId) {
        ToastUitl.showShort(resId);
    }

    @Override
    public void showShortToast(String txt) {
        ToastUitl.showShort(txt);
    }

    @Override
    public void showLongToast(String txt) {
        ToastUitl.showLong(txt);
    }

    @Override
    public void showLongToast(int resId) {
        ToastUitl.showLong(resId);
    }

    @Override
    public void showLoading() {
        if(mLoadingDialog!=null){
            mLoadingDialog.show();
        }else{
            initProgressDialog();
        }
    }

    @Override
    public void hideLoading() {
        if(mLoadingDialog != null) {
            mLoadingDialog.cancel();
        }
    }

    @Override
    public void showNetError() {

    }

    @Override
    public void finish() {
        getActivity().finish();
    }

    /**
     * 加载dialog 初始化
     */
    private void initProgressDialog(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loading, null);
        TextView loadingText = (TextView)view.findViewById(R.id.tv_loading_dialog_text);
        loadingText.setText("加载中...");
        mLoadingDialog = new Dialog(getContext(), R.style.dialog);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 跳转结果至easyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

}
